/*******************************************************************************

                                BIN2HDF, HDF2BIN, etc.

  Title:        Bin2hdf, Hdf2bin.
  Author:       Hanna Szoke
  Date:         Apr 25, 2005   
  Function:     Convert various file formats to/from .hdf.

*******************************************************************************/

#include "util.h"
#include "dims.h"
#include "cellparams.h"

void	bin2hdf_main(int argc, char *argv[])
{
     void	readEpixFile(char *, real *) ;	
     void	write_HDFnump(char *, real *) ;

     char	filename[MAXSTRING] ;
     char	pwd_name[MAXSTRING], out_name[MAXSTRING] ;
     real	*array ;		/* electron densities */
     void	strip_suffix(char *, char *, char *) ;

  /***********************************************************
   Check for right ballpark, say hello, identify file names.
  ***********************************************************/

     if (argc > optind+3) 	/* too many arguments */
	  ballpark(caller) ; 
     else if (argc == optind+3)
          sprintf(filename, "%s", argv[optind+2]) ;
     else {
	  prompt("\nPlease enter the name of an electron/voxel file: ") ;
          sscanf(terminp, "%s", filename) ;
	  sprintf(message, "Running %s on el/voxel file %s", caller, filename) ;
	  printTwice(message) ;
     }

     hello(caller) ;

  /************************************************************
  Fetch input conditions, set up array in physical space.
  Read the bin file and write the .hdf corresponding file.
  Output will be written in pwd rather than in directory from
  which the .bin files were read. 
  ************************************************************/

     readBasicInput() ;

     array = (real *) e_malloc(Nptotal*sizeof(real), caller) ;
          
     readEpixFile(filename, array) ;

     strip_path(filename, pwd_name) ;
     strip_suffix(pwd_name, BINSUF, out_name) ;
     strcat(out_name, HDFSUF) ;

     write_HDFnump(out_name, array) ;
}

void	hdf2bin_main(int argc, char *argv[])
{
     void	read_HDFnump(char *, real *);
     void	writeEpixFile(char *, real *) ;	        /* in qshare.c */
     void	strip_suffix(char *, char *, char *) ;

     char	hdf_filename[MAXSTRING] ;
     char	pwd_name[MAXSTRING], out_name[MAXSTRING] ;

     real	*xnump ;		/* electron densities */

  /***********************************************************
   Check for right ballpark, say hello, identify file names.
  ***********************************************************/

     if (argc > optind+3) 	/* too many arguments */
	  ballpark(caller) ; 
     else if (argc == optind+3)
          sprintf(hdf_filename, "%s", argv[optind+2]) ;
     else {
	  prompt("\nPlease enter the full name of the .hdf file: ") ;
          sscanf(terminp, "%s", hdf_filename) ;
     }

     hello(caller) ;

  /************************************************************
  Fetch input conditions, set up array in physical space.
  Read the .hdf file and write the .bin equivalent.
  ************************************************************/

     readBasicInput() ;

     sprintf(message, "Reading hdf file %s", hdf_filename) ;
     printTwice(message) ;

     xnump = (real *) e_malloc(Nptotal*sizeof(real), caller) ;

     read_HDFnump(hdf_filename, xnump) ;

  /************************************************************
  Output will be written in pwd rather than in directory from
  which the .bin file was read. 
  ************************************************************/

     strip_path(hdf_filename, pwd_name) ;
     strip_suffix(pwd_name, HDFSUF, out_name) ;
     strcat(out_name, BINSUF) ;

     sprintf(message, "Writing binary file to %s", out_name) ;
     printTwice(message) ;

     writeEpixFile(out_name, xnump) ;
}
